package com.lanren.familybookapp.ui.me.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lanren.common.interfaces.AdapterItemClickView;
import com.lanren.common.interfaces.DialogClickListener;
import com.lanren.common.utils.Utils;
import com.lanren.common.widget.TitleBarView;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.helper.DialogHelper;
import com.lanren.familybookapp.ui.me.adapter.ManagerAdapter;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IManagerModel;
import com.lanren.familybookapp.ui.me.model.BookManagerModel;
import com.lanren.familybookapp.ui.me.presenter.ManagerPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @ClassName BookManagerActivity
 * @Description TODO  账本管理
 * @Author chongheng.wang
 * @Date 2019/10/11 13:54
 * @Version 1.0
 */
public class BookManagerActivity extends BaseUiActivity<ManagerPresenter> implements IManagerModel.IManagerView {

    @BindView(R.id.title_view)
    TitleBarView titleView;
    @BindView(R.id.book_list_view)
    RecyclerView bookListView;
    @BindView(R.id.smart_view)
    SmartRefreshLayout smartView;
    @BindView(R.id.smart_header_view)
    ClassicsHeader smartHeaderView;
    @BindView(R.id.smart_footer_view)
    ClassicsFooter smartFooterView;

    private ManagerAdapter managerAdapter;
    private List<BookInfoBean> list = new ArrayList<>();

    private boolean isRefresh = false;
    private int page = 0;
    private int mUserUseBook;

    @Override
    public ManagerPresenter createPresenter() {
        return new ManagerPresenter(new BookManagerModel());
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_book_manager;
    }

    @Override
    public void doCreate(Bundle savedInstanceState) {
        initView();
        initData();
        initEvent();
    }

    public void initView() {
        bookListView.setLayoutManager(new LinearLayoutManager(this));
    }

    @SuppressLint("SetTextI18n")
    public void initData() {
        getData(true);// 获取列表数据

        managerAdapter = new ManagerAdapter(this, list);
        bookListView.setAdapter(managerAdapter);
    }

    public void initEvent() {
        titleView.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        titleView.setRightTxtListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick(v)) {
                    DialogHelper.getInstance().showInputDialog(BookManagerActivity.this, "账本名称", 10, new DialogHelper.OnClickListener() {
                        @Override
                        public void onClicks(String msg) {
                            mPresenter.addBook(msg);
                        }
                    });
                }
            }
        });

        smartView.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData(true);
            }
        });

        managerAdapter.setAdapterItemClickView(new AdapterItemClickView() {
            @Override
            public void onItemClick(int pointion) {
                BookEditActivity.startActivity(BookManagerActivity.this, 1, list.get(pointion));
            }

            @Override
            public void onItemClick(int pointion, View view) {
                if (Utils.isFastClick(view)) {
                    mUserUseBook = list.get(pointion).getId();
                    mPresenter.updateUseBook(list.get(pointion).getId());
                }
            }

            @Override
            public void onItemLongClick(final int pointion) {
                showMessageDialog("是否删除账本？", false, new DialogClickListener() {
                    @Override
                    public void onCancelClick(View v) {

                    }

                    @Override
                    public void onConfirmClick(View v) {
                        if (Utils.isFastClick(v)) {
                            mPresenter.delBook(list.get(pointion).getId());
                        }
                    }
                });
            }

            @Override
            public void onItemLongClick(int pointion, View view) {

            }
        });
    }

    @Override
    public void onSuccess(List<BookInfoBean> bookInfoBeans) {
        if (bookInfoBeans.size() > 0) {
            showComplete();
            if (isRefresh) {
                smartView.finishRefresh();
                list.clear();
            }
            list.addAll(bookInfoBeans);
            if (isRefresh) {
                managerAdapter.notifyDataSetChanged();
            } else {
                smartView.finishLoadMore();
                managerAdapter.notifyItemChanged(list.size() - 1);
            }
        } else {
            if (list.size() == 0) {
                showEmpty();
            } else {
                smartView.finishLoadMoreWithNoMoreData();
            }
        }
    }

    @Override
    public void showError(int code, String msg) {
        if (code == 10009) {
            if (list.size() == 0) {
                smartView.finishRefresh();
                showEmpty();
            } else {
                if (isRefresh) {
                    list.clear();
                    managerAdapter.notifyDataSetChanged();
                    showEmpty();
                }
                smartView.finishLoadMoreWithNoMoreData();
            }
        } else {
            smartView.finishRefresh();
            smartView.finishLoadMore();
            super.showError(code, msg);
        }
    }

    @Override
    public void addBookSuccess(String msg) {
        getData(true);
        showMessage(msg);
    }

    @Override
    public void useBookSuccess(String msg) {
        AppUserHelper.getInstance().getUser().setUserUseBook(mUserUseBook);
        showMessage(msg);
        managerAdapter.notifyDataSetChanged();
    }

    private void getData(boolean refresh) {
        isRefresh = refresh;
        if (refresh) {
            page = 0;
        }
        page++;
        mPresenter.getBookList(page);
    }
}
